package com.fesc.trade.demo.request;

import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fesc.trade.demo.model.BaseModel;
import com.fesc.trade.demo.util.AssertUtil;

/**
 * 商户入驻请求接口
 * @author prd-wfc
 * @version $Id: MerchantCreateRequest.java, v 0.1 2016年8月18日 下午6:35:30 prd-wfc Exp $
 */
public class MerchantCreateRequest extends BaseRequest {

    /** uid */
    private static final long   serialVersionUID = 8875769710960198702L;

    private BizContent          bizContent       = new BizContent();

    private Map<String, String> paraMap          = new TreeMap<String, String>();

    @Override
    public void validate() {

        super.validate();

        AssertUtil.assertNotBlank(bizContent.externalId, "external_id should not be NULL!");

        AssertUtil.assertNotBlank(bizContent.name, "name should not be NULL!");

        AssertUtil.assertNotBlank(bizContent.aliasName, "alias_name should not be NULL!");

        AssertUtil.assertNotBlank(bizContent.servicePhone, "service_phone should not be NULL!");

        AssertUtil.assertNotBlank(bizContent.categoryId, "category_id should not be NULL!");

        AssertUtil.assertNotBlank(bizContent.source, "source should not be NULL");

    }

    public String getExternalId() {
        return bizContent.externalId;
    }

    public MerchantCreateRequest setExternalId(String externalId) {
        bizContent.externalId = externalId;
        return this;
    }

    public String getName() {
        return bizContent.name;
    }

    public MerchantCreateRequest setName(String name) {
        bizContent.name = name;
        return this;
    }

    public String getAliasName() {
        return bizContent.aliasName;
    }

    public MerchantCreateRequest setAliasName(String aliasName) {
        bizContent.aliasName = aliasName;
        return this;
    }

    public MerchantCreateRequest setServicePhone(String servicePhone) {
        bizContent.servicePhone = servicePhone;
        return this;
    }

    public String getServicePhone() {
        return bizContent.servicePhone;
    }

    public MerchantCreateRequest setContactName(String contactName) {
        bizContent.contactName = contactName;
        return this;
    }

    public String getContactName() {
        return bizContent.contactName;
    }

    public String getContactPhone() {
        return bizContent.contactPhone;
    }

    public MerchantCreateRequest setContactPhone(String contactPhone) {
        bizContent.contactPhone = contactPhone;
        return this;
    }

    public String getContactMobile() {
        return bizContent.contactMobile;
    }

    public MerchantCreateRequest setContactMobile(String contactMobile) {
        bizContent.contactMobile = contactMobile;
        return this;
    }

    public String getContactEmail() {
        return bizContent.contactEmail;
    }

    public MerchantCreateRequest setContactEmail(String contactEmail) {
        bizContent.contactEmail = contactEmail;
        return this;
    }

    public String getCategoryId() {
        return bizContent.categoryId;
    }

    public MerchantCreateRequest setCategoryId(String categoryId) {
        bizContent.categoryId = categoryId;
        return this;
    }

    public String getMemo() {
        return bizContent.memo;
    }

    public MerchantCreateRequest setMemo(String memo) {
        bizContent.memo = memo;
        return this;
    }

    public String getSource() {
        return bizContent.source;
    }

    public MerchantCreateRequest setSource(String source) {
        bizContent.source = source;
        return this;
    }

    public String getExternalInfo() {
        return bizContent.externalInfo;
    }

    public MerchantCreateRequest setExternalInfo(String externalInfo) {
        bizContent.externalInfo = externalInfo;
        return this;
    }

    public Map<String, String> buildParaMap() {
        super.setBizContent(bizContent.buildBizContentStr());

        if (StringUtils.isNotBlank(acquirerId)) {
            paraMap.put("acquirer_id", acquirerId);
        }
        if (StringUtils.isNotBlank(signType)) {
            paraMap.put("sign_type", signType);
        }
        if (StringUtils.isNotBlank(timestamp)) {
            paraMap.put("timestamp", timestamp);
        }
        if (StringUtils.isNotBlank(appAuthToken)) {
            paraMap.put("app_auth_token", appAuthToken);
        }
        if (StringUtils.isNotBlank(walletType)) {
            paraMap.put("wallet_type", walletType);
        }
        if (StringUtils.isNotBlank(super.bizContent)) {
            paraMap.put("biz_content", super.bizContent);
        }

        return paraMap;

    }

    /** 业务参数 */
    private static class BizContent extends BaseModel {

        /**  */
        private static final long serialVersionUID = 1L;

        // 商户外部编号，一个受理机构下下面唯一
        @JSONField(name = "external_id")
        private String            externalId;

        // 商户名称
        @JSONField(name = "name")
        private String            name;

        // 商户简称
        @JSONField(name = "alias_name")
        private String            aliasName;

        // 客服电话
        @JSONField(name = "service_phone")
        private String            servicePhone;

        // 联系人名称
        @JSONField(name = "contact_name")
        private String            contactName;

        // 联系人电话
        @JSONField(name = "contact_phone")
        private String            contactPhone;

        // 联系人手机号
        @JSONField(name = "contact_mobile")
        private String            contactMobile;

        // 联系人邮箱
        @JSONField(name = "contact_email")
        private String            contactEmail;

        // 经营类目id
        @JSONField(name = "category_id")
        private String            categoryId;

        // 商户备注
        @JSONField(name = "memo")
        private String            memo;

        //商户来源标识
        @JSONField(name = "source")
        private String            source;

        // 扩展信息
        @JSONField(name = "external_info")
        private String            externalInfo;

        public String buildBizContentStr() {

            Map<String, Object> bizParaMap = new TreeMap<String, Object>();

            if (StringUtils.isNotBlank(externalId)) {
                bizParaMap.put("external_id", externalId);
            }
            if (StringUtils.isNotBlank(name)) {
                bizParaMap.put("name", name);
            }
            if (StringUtils.isNotBlank(aliasName)) {
                bizParaMap.put("alias_name", aliasName);
            }
            if (StringUtils.isNotBlank(servicePhone)) {
                bizParaMap.put("service_phone", servicePhone);
            }
            if (StringUtils.isNotBlank(contactName)) {
                bizParaMap.put("contact_name", contactName);
            }
            if (StringUtils.isNotBlank(contactPhone)) {
                bizParaMap.put("contact_phone", contactPhone);
            }
            if (StringUtils.isNotBlank(contactMobile)) {
                bizParaMap.put("contact_mobile", contactMobile);
            }
            if (StringUtils.isNotBlank(contactEmail)) {
                bizParaMap.put("contact_email", contactEmail);
            }
            if (StringUtils.isNotBlank(categoryId)) {
                bizParaMap.put("category_id", categoryId);
            }
            if (StringUtils.isNotBlank(memo)) {
                bizParaMap.put("memo", memo);
            }
            if (StringUtils.isNotBlank(source)) {
                bizParaMap.put("source", source);
            }
            if (StringUtils.isNotBlank(externalInfo)) {
                bizParaMap.put("external_info", externalInfo);
            }

            return JSON.toJSONString(bizParaMap);
        }
    }

}
